package com.ace.modular.system.controller;

import com.ace.common.page.PageInfoBT;
import com.ace.common.warpper.BaseControllerWarpper;
import com.ace.core.suppoort.HttpKit;
import com.baomidou.mybatisplus.plugins.Page;
import org.omg.CORBA.Object;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created by com.ace on 2018/3/9.
 */
@Component
@Scope(scopeName = WebApplicationContext.SCOPE_REQUEST)
public class BaseController {
    protected static String SUCCESS = "SUCCESS";
    protected static String ERROR = "ERROR";

    protected static String REDIRECT = "redirect:";
    protected static String FORWARD = "forward:";

    protected HttpServletRequest getHttpServletRequest(){
        return HttpKit.getRequest();
    }

    protected HttpServletResponse getHttpServletResponse(){
        return HttpKit.getResponse();
    }

    protected HttpSession getSession(){
        return HttpKit.getRequest().getSession();
    }

    protected String getPara(String name){
        return HttpKit.getRequest().getParameter(name);
    }

    protected void setAttr(String name, Object value){
        HttpKit.getRequest().setAttribute(name, value);
    }

    protected  Integer fgetSystemInvokeCount(){
        return (Integer) this.getHttpServletRequest().getServletContext().getAttribute("systemCount");
    }

    /**
     * 把Service层的分页信息，封装为bootstrap table通用的分页封装
     *
     */
    protected <T> PageInfoBT<T> packForBT(Page<T> page){
        return new PageInfoBT<T>(page);
    }

    /**
     * 包装一个list，让list增加额外属性
     */
    protected java.lang.Object warpObject(BaseControllerWarpper warpper){
        return warpper.warp();
    }

    /**
     * 删除cookie
     */

    protected void deleteCookieByName(String cookieName){
        Cookie[] cookies = this.getHttpServletRequest().getCookies();
        for(Cookie cookie : cookies){
            if(cookie.getName().equals(cookieName)){
                Cookie temp = new Cookie(cookie.getName(), "");
                temp.setMaxAge(0);
                this.getHttpServletResponse().addCookie(temp);
            }
        }
    }
}
